package table_user

import (
	"gitee.com/klein-x/biz/model/mysql/table"
	"gitee.com/klein-x/biz/module/db/mysql"
	"gitee.com/klein-x/biz/xutils"
	"gorm.io/gorm"
)

type UserInfoWithPaper struct {
	ID         string         `gorm:"primaryKey;type:char(36);unique;not null;comment:'交互记录ID'"`
	UserUUID   string         `gorm:"type:char(36);not null;comment:'用户UUID'"`
	PaperID    string         `gorm:"type:char(36);not null;comment:'试卷ID'"`
	QuestionID string         `gorm:"type:char(36);not null;comment:'题目ID'"`
	IsFavorite string         `gorm:"type:char(1);not null;default:'0';comment:'是否收藏, 否 0, 是 1'"`
	Note       string         `gorm:"type:text;comment:'笔记内容，可为空'"`
	DeletedAt  gorm.DeletedAt `gorm:"comment:'软删除时间'"`
}

// TableName 实现 GORM 的 TableName 接口，自定义表名
func (uqi *UserInfoWithPaper) TableName() string {
	return "user_info_with_paper"
}

func FavoriteGetByUser(userUUID string) (*[]UserInfoWithPaper, error) {
	db := mysql.GetGlobalDB() // 获取全局数据库连接实例

	var usersInfo []UserInfoWithPaper
	return table.SelectCheck(&usersInfo, db.Where("user_uuid = ? AND is_favorite = '1'", userUUID).Find(&usersInfo))
}
func NoteGetByUser(userUUID string) (*[]UserInfoWithPaper, error) {
	db := mysql.GetGlobalDB() // 获取全局数据库连接实例

	var usersInfo []UserInfoWithPaper
	return table.SelectCheck(&usersInfo, db.Where("user_uuid = ? AND (Note IS NOT NULL AND Note <> '')", userUUID).Find(&usersInfo))
}

func FavoriteGetByUserAndPaperAndQuestion(uuid string, paperId, questionId string) (*UserInfoWithPaper, error) {
	db := mysql.GetGlobalDB() // 获取全局数据库连接实例

	var usersInfo UserInfoWithPaper

	return table.SelectCheck(&usersInfo, db.Where("user_uuid = ? AND paper_id = ? AND question_id = ?", uuid, paperId, questionId).First(&usersInfo))
}

func NoteGetByUserAndPaperAndQuestion(userUUID string, paperId, questionId string) (*UserInfoWithPaper, error) {
	db := mysql.GetGlobalDB() // 获取全局数据库连接实例

	var usersInfo UserInfoWithPaper

	return table.SelectCheck(&usersInfo,
		db.Where("user_uuid = ? AND paper_id = ? AND question_id = ? AND note <> ?",
			userUUID, paperId, questionId, "").First(&usersInfo),
	)
}

func FavoriteSetByUserAndPaperAndQuestion(userUUID string, paperID string, questionID string, IsFavorite bool) error {
	db := mysql.GetGlobalDB()

	var count int64
	err := db.Model(&UserInfoWithPaper{}).Where("user_uuid = ? AND paper_id = ? AND question_id = ?", userUUID, paperID, questionID).Count(&count).Error
	if err != nil {
		return err
	}

	var isF string
	if IsFavorite {
		isF = "1"
	} else {
		isF = "0"
	}

	if count == 0 {
		var info UserInfoWithPaper
		uuid, err := xutils.RandomUUID()
		if err != nil {
			return err
		}
		info.ID = uuid
		info.UserUUID = userUUID
		info.PaperID = paperID
		info.QuestionID = questionID
		info.IsFavorite = isF
		return table.InsertCheck(db.Create(&info))
	} else {
		return table.UpdateCheck(db.Model(&UserInfoWithPaper{}).Where("user_uuid = ? AND paper_id = ? AND question_id = ?", userUUID, paperID, questionID).Update("is_favorite", isF))
	}
}

func NoteSetByUserAndPaperAndQuestion(userUUID string, paperID string, questionID string, note string) error {
	db := mysql.GetGlobalDB()

	var count int64
	err := db.Model(&UserInfoWithPaper{}).Where("user_uuid = ? AND paper_id = ? AND question_id = ?", userUUID, paperID, questionID).Count(&count).Error
	if err != nil {
		return err
	}

	if count == 0 {
		var info UserInfoWithPaper
		uuid, err := xutils.RandomUUID()
		if err != nil {
			return err
		}
		info.ID = uuid
		info.UserUUID = userUUID
		info.PaperID = paperID
		info.QuestionID = questionID
		info.Note = note
		return table.InsertCheck(db.Create(&info))
	} else {
		return table.UpdateCheck(db.Model(&UserInfoWithPaper{}).Where("user_uuid = ? AND paper_id = ? AND question_id = ?", userUUID, paperID, questionID).Update("note", note))
	}
}
